package vn.info.vietnaminfo;

import vn.info.vietnaminfo.dialog.ShowDialog;
import vn.info.vietnaminfo.sync.GetDataKhoiDongApp;
import vn.info.vietnaminfo.utli.GlobalVariable;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class VietNamInfoActivity extends Activity {

	private Context mContext;
	private ViewFlipper mViewFlipper;
	private Animation leftIn, leftOut, rightIn, rightOut, show, hidden;
	private Button btnDongDSTP;
	private ImageView tabPlaces, tabBookmark, btnChon64TP;
	private LinearLayout menuTabPlaces, menuTabBookmark, horizontalPager, pagerIcon, layout64TP, layoutHome;
	private ListView list64TinhThanh, listDanhDau;
	private GetDataKhoiDongApp getDatachoApp;
	private TextView t;
	private ShowDialog dialog;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_vietnam_info_activity);

		mContext 			= this;
		menuTabPlaces 		= (LinearLayout)findViewById(R.id.menu_tab_places);
		menuTabBookmark 	= (LinearLayout)findViewById(R.id.menu_tab_bookmark);
		horizontalPager		= (LinearLayout)findViewById(R.id.horizontal_pager);
		pagerIcon 			= (LinearLayout)findViewById(R.id.horizontal_pager_icon);
		layout64TP			= (LinearLayout)findViewById(R.id.layout_64_tp);
		layoutHome			= (LinearLayout)findViewById(R.id.layout_home);
		tabPlaces 			= (ImageView)findViewById(R.id.tab_places_selected);
		tabBookmark 		= (ImageView)findViewById(R.id.tab_bookmark_selected);
		btnChon64TP			= (ImageView)findViewById(R.id.btn_chon_64_tp);
		leftIn 				= AnimationUtils.loadAnimation(this, R.anim.push_left_in);
		leftOut 			= AnimationUtils.loadAnimation(this, R.anim.push_left_out);
		rightIn 			= AnimationUtils.loadAnimation(this, R.anim.push_right_in);
		rightOut 			= AnimationUtils.loadAnimation(this, R.anim.push_right_out);
		show				= AnimationUtils.loadAnimation(this, R.anim.slide_down_to_up);
		hidden				= AnimationUtils.loadAnimation(this, R.anim.slide_up_to_down);
		mViewFlipper 		= (ViewFlipper) findViewById(R.id.view_flipper);
		list64TinhThanh		= (ListView)findViewById(R.id.list_thanh_pho);
		listDanhDau			= (ListView)findViewById(R.id.list_book_mark);
		btnDongDSTP			= (Button)findViewById(R.id.btn_dong_ds_tp);

		menuTabPlaces.setOnClickListener(clickListener);
		menuTabBookmark.setOnClickListener(clickListener);
		btnDongDSTP.setOnClickListener(clickListener);
		btnChon64TP.setOnClickListener(clickListener);
		tabBookmark.setBackgroundColor(Color.TRANSPARENT);
		menuTabPlaces.setEnabled(false);

		// Get độ phân giải màn hình
		Display display = getWindowManager().getDefaultDisplay(); 
		GlobalVariable.SCREEN_WIDTH 	= display.getWidth();
		GlobalVariable.SCREEN_HEIGHT 	= display.getHeight();

		// Get giá trị device id của thiết bị
		GlobalVariable.DEVICE_ID = GlobalVariable.getDeviceId(mContext);

		GlobalVariable.GET_DATA_CHO_APP = false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.viet_nam_info, menu);
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		if(!GlobalVariable.GET_DATA_CHO_APP)
		{
			getDatachoApp = new GetDataKhoiDongApp(mContext, list64TinhThanh, listDanhDau, horizontalPager, pagerIcon, layoutHome);
			getDatachoApp.execute();
		}
	}

	// Xử lý kết thúc
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		System.gc();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			ShowDialog show = new ShowDialog(mContext);
			show.ThongBaoThoatKhoiUngDung(this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private View.OnClickListener clickListener = new OnClickListener()
	{
		public void onClick(View click)
		{
			if(menuTabPlaces == click)
			{
				tabPlaces.setBackgroundColor(Color.WHITE);
				tabBookmark.setBackgroundColor(Color.TRANSPARENT);

				menuTabBookmark.setEnabled(true);
				menuTabPlaces.setEnabled(false);

				mViewFlipper.setInAnimation(rightIn);
				mViewFlipper.setOutAnimation(rightOut);
				mViewFlipper.showPrevious();
			}
			else if(menuTabBookmark == click)
			{
				tabBookmark.setBackgroundColor(Color.WHITE);
				tabPlaces.setBackgroundColor(Color.TRANSPARENT);

				menuTabPlaces.setEnabled(true);
				menuTabBookmark.setEnabled(false);

				mViewFlipper.setInAnimation(leftIn);
				mViewFlipper.setOutAnimation(leftOut);
				mViewFlipper.showNext();
			}
			else if(btnChon64TP == click)
			{
				ShowDanhSanhChonTP();
			}
			else if(btnDongDSTP == click)
			{
				dongDanhSachThanhPho();
			}
		}
	};

	public void ShowDanhSanhChonTP()
	{
		layout64TP.setVisibility(View.VISIBLE);
		layout64TP.startAnimation(show);
		btnChon64TP.setEnabled(false);
	}

	public void dongDanhSachThanhPho()
	{
		layout64TP.startAnimation(hidden);
		new MainCountDown(500, 500).start();
	}

	public class MainCountDown extends CountDownTimer
	{
		public MainCountDown(long millisInFuture, long countDownInterval)
		{
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish()
		{
			layout64TP.setVisibility(View.GONE);
			btnChon64TP.setEnabled(true);
		}
		@Override
		public void onTick(long millisUntilFinished) {}
	}


}
